package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.PaymentContract;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 收款子表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-12-12
 */
@Mapper
public interface PaymentContractMapper extends BaseMapper<PaymentContract> {

    @Select("select dpc.money from dragon_payment_contract dpc " +
            "left join dragon_customer_payment dcp on dcp.id = dpc.payment_id " +
            "where dpc.contract_id = #{contractId} and dpc.plan_id = #{planId} and dcp.status = 0")
    List<PaymentContract> selectMoneyByContractId(Integer planId, Integer contractId);

    @Select("select dpc.*, dc.name as contractName, dp.plan_money as planMoney, " +
            " dcc.customer_name as customerName ,dp.actual_plan_money as actualPlanMoney from dragon_payment_contract dpc " +
            "left join dragon_plancollect dp on dpc.plan_id = dp.id " +
            "left join dragon_customer dcc on dcc.id = dp.customer_id " +
            "left join dragon_contract dc on dc.id = dpc.contract_id where dpc.payment_id = #{id}")
    List<PaymentContract> selectByPaymentId(Integer id);

    @Select("select dpc.*, db.plan_money as planMoney, " +
            " dcc.customer_name as customerName ,db.payment_money as actualPlanMoney from dragon_payment_contract dpc " +
            "left join dragon_bill db on dpc.bill_id = db.id " +
            "left join dragon_customer dcc on dcc.id = db.customer_id " +
            " where dpc.payment_id = #{id}")
    List<PaymentContract> selectContractByPaymentId(Integer id);

    @Select("SELECT M.*  FROM (select dpc.*,dcp.payment_status as paymentStatus from dragon_payment_contract dpc " +
            "LEFT JOIN dragon_customer_payment dcp on dpc.payment_id  = dcp.id )M " +
            "${ew.customSqlSegment} ")
    Page<PaymentContract> selectpageByWarpper(Page<PaymentContract> paymentPage, @Param(Constants.WRAPPER) QueryWrapper<PaymentContract> wrapper);
}
